home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-12-02 | 2.8 KB | 138 lines | [TEXT/????] |
- NB. native (DOS) file read/write utilities
- NB.
- NB. 'string' verbs write out text delimited by CRLF,
- NB. and read in text delimited by LF.
- NB.
- NB. read verbs take an optional left argument:
- NB. a single number is: length of read
- NB. two numbers are: index of read, length of read
- NB.
- NB. file write verbs return number of characters written
- NB.
- NB. dat nappend file append
- NB. dat nappends file append string
- NB. nexist file return 1 if file exists
- NB. opt nmread file read file as matrix
- NB. opt nread file read file
- NB. opt nreads file read string
- NB. opt nrread file read records
- NB. opt nvread file read file as boxed vector
- NB. dat nwrite file write file
- NB. dat nwrites file write string
-
- t=. 'nappend nappends nexist nmread nread nreads nrread'
- t=. t,' nvread nwrite nwrites'
- SCRIPTNAMES=: t
-
- NB. nappend
- nappend=: 0 : 0
- :
- r=. '0' ". 'x. 1!:3 <>y.'
- >(#r){(#x.);'write error'
- )
-
- NB. nappends
- nappends=: 0 : 0
- :
- x=. ,x.,"1 CRLF
- $.=. (1<#$x.)}.$.
- x=. (_2*CRLF e.~_1{x.)}.toCRLF x
- r=. '0' ". 'x 1!:3 <>y.'
- >(#r){(#x);'write error'
- )
-
- NB. nexist
- nexist=: '0'&".@('>: $ 1!:11 '''&,)@(,&''';0 0')
-
- NB. nmread
- NB. read records as matrix
- NB. x. = optional index, length
- nmread=: 0 : 0
- 1e9 nmread y.
- :
- s=. 1!:4 y =. <>y.
- 'bl'=. _2{.x.
- b=. (l<0){b,s+1
- l=. (s-b)<.|l
- d=. toLF 1!:11 y,<b,l
- d=. d -. 26{a.
- ] ;._2 d,LF #~ LF ~: {:d
- )
-
- NB. nread
- NB. read file, x. = optional index, length
- nread=: 0 : 0
- 1e9 nread y.
- :
- s=. 1!:4 y =. <>y.
- 'bl'=. _2{.x.
- b=. (l<0){b,s+l
- l=. (s-b)<.|l
- 1!:11 y,<b,l
- )
-
- NB. nreads
- NB. read file as script, x. = index, length
- nreads=: 0 : 0
- 1e9 nreads y.
- :
- s=. 1!:4 y =. <>y.
- 'bl'=. _2{.x.
- b=. (l<0){b,s+1
- l=. (s-b)<.|l
- d=. toLF 1!:11 y,<b,l
- d }.~ -+/(10 26{a.)e._2{.d
- )
-
- NB. nrread
- NB. read records from flat file
- NB. x. = optional record index, number of records
- nrread=: 0 : 0
- 1e9 nrread y.
- :
- max=. 1!:4 y =. <>y.
- len=. 0 * blk=. 10000<.max
- dat=. 1!:11 y,<0,blk
- $.=. $.}.~blk>cls =. dat i.CR
- 'not organized in records' [ $.=.''
- len=. >:cls+LF=(cls+1){dat,' '
- max=. <.max%len
- 'bl'=. _2{.x.
- b=. (<:max) <. (l<0){b,max+l
- l=. (max-b)<.|l
- dat=. (l,len)$ 1!:11 y,<len * b,l
- dat=. (cls-len)}."1 dat
- )
-
- NB. nvread
- NB. read records as boxed vector
- NB. x. = optional index, length
- nvread=: 0 : 0
- 1e9 nvread y.
- :
- s=. 1!:4 y =. <>y.
- 'bl'=. _2{.x.
- b=. (l<0){b,s+1
- l=. (s-b)<.|l
- d=. toLF 1!:11 y,<b,l
- d=. d }.~ -(26{a.) = {:d
- <;._2 d,LF #~ LF ~: {: d
- )
-
- NB. nwrite
- nwrite=: 0 : 0
- :
- r=. '0' ". 'x. 1!:2 <>y.'
- >(#r){(#x.);'write error'
- )
-
- NB. nwrites
- nwrites=: 0 : 0
- :
- x=. ,x.,"1 CRLF
- $.=. (1<#$x.)}.$.
- x=. (_2*CRLF e.~_1{x.)}. toCRLF x
- r=. '0' ". 'x 1!:2 <>y.'
- >(#r){(#x);'write error'
- )
-